package com.example.springboot_demo.demos.web.book_work.Server.ServerImpl;

import com.example.springboot_demo.Common.JSONUtil;
import com.example.springboot_demo.demos.web.book_work.Enum.BookStatus;
import com.example.springboot_demo.demos.web.book_work.Exception.LibraryException;
import com.example.springboot_demo.demos.web.book_work.Mapper.BookMapper;
import com.example.springboot_demo.demos.web.book_work.Mapper.BookUserMapper;
import com.example.springboot_demo.demos.web.book_work.Pojo.dto.BrrowBookDto;
import com.example.springboot_demo.demos.web.book_work.Pojo.dto.ReturnBookDto;
import com.example.springboot_demo.demos.web.book_work.Pojo.entity.Book;
import com.example.springboot_demo.demos.web.book_work.Pojo.vo.UserVo;
import com.example.springboot_demo.demos.web.book_work.Server.BookServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

@Service
public class BookServiceImpl implements BookServer {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookUserMapper bookUserMapper;

    @Override
    public List<Book> getBookList() {
        return bookMapper.selectAllBooks();
    }



    @Override
    public Boolean borrowBook(BrrowBookDto brrowBookDto) {
        //获取当前所借书籍的信息
        Book book = bookMapper.selectBookById(brrowBookDto.getBookId());
        //获取当前用户信息
        UserVo user = bookUserMapper.getUserById(brrowBookDto.getUserId());

        List<Book> list = JSONUtil.fromJsonString(user.getBorrowedBooks(), List.class);
        //获取状态
        BookStatus status = book.getStatus();
        //书籍状态是否为AVAILABLE
        if (status.equals(BookStatus.AVAILABLE) &&
                book.getStock() >= 0 &&
                list.size() < 3){

            book.setStock(book.getStock() - 1);
            List<Book> borrowedBooks = list;
            borrowedBooks.add(book);
            bookMapper.updateBook(book);
            bookUserMapper.UpdateUser(user.getName(), JSONUtil.toJsonString(borrowedBooks), user.getUserId());
            return true;
        }

        if (list.size() >= 3){
            throw new LibraryException("当前所借书籍已超上限");
        }

        if (status.equals(BookStatus.BORROWED)){
            throw new LibraryException("当前书籍已被全部借出");
        }

        if (status.equals(BookStatus.UNDER_MAINTENANCE)){
            throw new LibraryException("当前库存不足");
        }
        return false;
    }

    @Override
    public Double returnBook(ReturnBookDto returnBookDto) {
        //获取当前用户信息
        UserVo user = bookUserMapper.getUserById(returnBookDto.getUserId());
        //获取用户想要还的书籍
        Book book = bookMapper.selectBookById(returnBookDto.getBookId());
        //拿到当前用户所有的书籍
        List<LinkedHashMap> list = JSONUtil.fromJsonString(user.getBorrowedBooks(), List.class);

        if (list.size() <= 0){
            throw new LibraryException("当前用户没有书籍无需还书");
        }
        for (int i = 0; i < list.size(); i++) {
            Integer byIdBookId = (Integer) list.get(i).get("id");
            if (byIdBookId.longValue() == book.getId()){
                list.remove(i);
                System.out.println(list);
                book.setStock(book.getStock() + 1);
                book.setStatus(BookStatus.AVAILABLE);
                bookMapper.updateBook(book);
                bookUserMapper.UpdateUser(user.getName(), JSONUtil.toJsonString(list), user.getUserId());
                return Math.max(0.0, returnBookDto.getOverdueDays() * 1.0);
            }
        }

        return 0.0;
    }
}
